﻿<?php
// ------------------------------------------------------------------
// NEEDED STUBS FOR THIS EXAMPLE
// ------------------------------------------------------------------


// this will include all needed files to be able to build a Request
include_once($CONF["STUBS_FOLDER"]."Map24Routing/Map24Routing.php5");


// ------------------------------------------------------------------
// NEEDED PARAMETERS FOR THIS REQUEST
// ------------------------------------------------------------------


// the start address (coordinates)
$sLongitude = '520.97131347656';
$sLatitude 	= '3008.740234375';

// the destination address (coordinates)
$dLongitude = '799.04816';
$dLatitude 	= '3150.0925';


// Array of ViaPoints

// 1st via point
$viaIdx=0;
$viaPoints[$viaIdx]['Longitude']='417.54556';
$viaPoints[$viaIdx]['Latitude']='3056.8179';
$viaPoints[$viaIdx]['TransitRadius']=500;
// 2nd via point
$viaIdx++;
$viaPoints[$viaIdx]['Longitude']='598.3';
$viaPoints[$viaIdx]['Latitude']='3213.0068';
$viaPoints[$viaIdx]['TransitRadius']=2000;
// 3rd via point
$viaIdx++;
$viaPoints[$viaIdx]['Longitude']='514.048';
$viaPoints[$viaIdx]['Latitude']='3007.8325';
$viaPoints[$viaIdx]['TransitRadius']=1000;

// the language for the route description
$descriptionLanguage = "DE";		//EN


// ------------------------------------------------------------------
// BUILDING THE REQUEST
// ------------------------------------------------------------------


// definition of the request header with Map24ID and SessionID
$header = new RequestHeader();
$header->Map24ID  = $CONF['MAP24_ID'];
$header->ClientID = $CONF['SESSION_ID'];

// definition of the request body
$request = new CalculateRouteRequest();

// define the Start Address
$request->Start = new CoordinateAndAddress();
$request->Start->Coordinate = new Coordinate();
$request->Start->Coordinate->Longitude = $sLongitude;
$request->Start->Coordinate->Latitude = $sLatitude;

//Create a new Array of ViaPoints Objects and fill it with the data of the viaPoints Array
for($viaIdx=0;$viaIdx<count($viaPoints);$viaIdx++)
{
	$viaPointsAry[$viaIdx]= new CalculateRouteRequestViaPoint();
	$viaPointsAry[$viaIdx]->Coordinate= new Coordinate();
	$viaPointsAry[$viaIdx]->Coordinate->Longitude = $viaPoints[$viaIdx]['Longitude'];
	$viaPointsAry[$viaIdx]->Coordinate->Latitude = $viaPoints[$viaIdx]['Latitude'];
	$viaPointsAry[$viaIdx]->TransitRadius->$viaPoints[$viaIdx]['TransitRadius'];
}
if(count($viaPoints)>0)	{ $request->ViaPoints=$viaPointsAry; }

// define the Destination Address
$request->Destination = new CoordinateAndAddress();
$request->Destination->Coordinate = new Coordinate();
$request->Destination->Coordinate->Longitude = $dLongitude;
$request->Destination->Coordinate->Latitude = $dLatitude;

// define the Description Language
$request->DescriptionLanguage = $descriptionLanguage;


// ------------------------------------------------------------------
// EXECUTE THE REQUEST
// ------------------------------------------------------------------


// in this object the SoapClient will be initiated and executed
$routing = new Map24Routing($CONF['MAPTP_SYSTEM'],$CONF['MAP24_ID'],$CONF['SESSION_ID']);
$response = $routing->calculateRoute($header,$request);


// ------------------------------------------------------------------
// OUTPUT THE RESULT
// ------------------------------------------------------------------


if( isset($response->Route) )
{
	foreach ($response->Route->Segments as $result)
	{
		$counter++;

		$resultString .= "<div style='border:1px solid black;margin:2px 0px;padding:5px;'>".$counter.". ";
		foreach ( $result->Descriptions as $description )
		{
			$resultString .= $description->Text."<br /><br />";
		}
		$resultString .= "</div>";
	}

	$RouteID=$response->Route->RouteID;
	// save the RouteID in a cookie (needed to show the route later)

	$t=date("U")+3600;
	SetCookie("RouteID",$RouteID,$t);
	// link to show a route on the map
	$resultString.=	"<br/><br/><a href=\"code_source.php?sid=" . $CONF['SESSION_ID'] . "&src=Map24MapletRemoteControl_showRoute.php5\" target=\"codeframe\"> MRC-Command to show the route on the map</a>";

}
else
{
	$resultString= "No Answer from the Webserver";
}

?>